<?php
/**
 * Created by: zhaoyi (834574377@qq.com)
 * Time: 2022/5/12 11:55
 * Blog: https://www.yuque.com/huangzhenlian
 */

declare(strict_types=1);

namespace App\Controller;

use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminUserService;
use App\Service\ApiService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use Psr\Http\Message\ResponseInterface as PsrResponseInterface;
use Hyperf\Cache\Annotation\Cacheable;

class AbstractController
{
    #[Inject]
    protected ValidatorFactoryInterface $validationFactory;

    #[Inject]
    protected RequestInterface $request;

    #[Inject]
    protected ResponseInterface $response;

    #[Inject]
    protected ApiService $api_service;

    /**
     * @param array $data
     * @param string $msg
     * @param int $code
     * @param bool $status
     * @return PsrResponseInterface
     */
    protected function returnSuccessJson(
        array  $data = [],
        string $msg = '',
        int    $code = 200,
        bool   $status = true
    ): PsrResponseInterface
    {
        !$msg && $msg = $this->request->isMethod('GET') ? '请求成功' : '操作成功';

        return $this->response->json($this->api_service->encryptData(compact('data', 'msg', 'status', 'code')));
    }

    protected function returnErrorJson(
        string $msg = '操作失败',
        int    $code = 500,
        array  $data = [],
        bool   $status = false
    ): PsrResponseInterface
    {
        return $this->response->json($this->api_service->encryptData(compact('data', 'msg', 'status', 'code')));
    }

    protected function returnSuccessJsonWithoutEncrypt(
        array  $data = [],
        string $msg = '',
        int    $code = 200,
        bool   $status = true
    ): PsrResponseInterface
    {
        !$msg && $msg = $this->request->isMethod('GET') ? '请求成功' : '操作成功';

        return $this->response->json(compact('data', 'msg', 'status', 'code'));
    }

    protected function returnErrorJsonWithoutEncrypt(
        string $msg = '操作失败',
        int    $code = 500,
        array  $data = [],
        bool   $status = false
    ): PsrResponseInterface
    {
        return $this->response->json(compact('data', 'msg', 'status', 'code'));
    }

    public function getstoreid()
    {
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();
        $store = Store::where('admin_id', $admin_user['admin_user_id'])->select('store_id')
            ->first() ?: [];
        return $store['store_id'];
    }

    public function getadminid()
    {
        $admin_user = AdminUserService::getInstance()
            ->getAdminUser();

        return $admin_user['admin_user_id'];
    }

    protected function postApiJson($api, $param, $header = ['Content-Type: application/json'])
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $api,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => json_encode($param),
            CURLOPT_HTTPHEADER => $header,
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response, true);
    }

    /**
     * 公用 get 请求
     */
    private function getApi($api, $param, $header = ['Content-Type: application/json'])
    {
        $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_URL => $api . '?' . http_build_query($param),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 60,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => $header,
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response, true);
    }

    public function adminopen($door_type, $door_id, $time)
    {
        switch ($door_type) {
            case '0':
                //版本1
                $api = 'https://zly.qile168.net/api/index/postapi';
                $access_token = 1;
                $param = ['a' => 1, 'b' => 2];
                $header = [
                    'Content-Type: application/json',
                    'Access-Token:' . $access_token
                ];
                $res = self::postApiJson($api, $param, $header);
                if ($res['code'] == 200) {
                    return true;
                } else {
                    return false;
                }

                break;
            case '1':
                //版本2
                return true;
                break;
            case '2':
                //版本3
                return true;
                break;

            default:
                //版本1
                return true;
                break;
        }

    }

}